<template>
<div class="app-container">
    <el-input
        placeholder="输入关键字进行过滤"
        v-model="filterText">
    </el-input>

    <el-tree
        class="filter-tree"
        :data="data"
        @node-click="nodeClick"
        :props="defaultProps"
        node-key="areaId"
        :expand-on-click-node="false"
        default-expand-all
        :filter-node-method="filterNode"
        ref="tree">
    </el-tree>
</div>
  </template>

  <script>
  import { listArea, getArea, delArea, addArea, updateArea } from "@/api/area/area";

  export default {
    name: "Area",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 区域信息表格数据
        areaList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 15,
          AreaName: null,
          AreaDesc: null,
          ParentId: null,
          OrderBy: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        },
        defaultProps: {
          children: 'list',
          label: 'areaName'
        },
        data:null,
      };
    },
    watch: {
      filterText(val) {
        this.$refs.tree.filter(val);
      }

    },
    created() {
      this.getList();
    },
    methods: {
    filterNode(value, data) {
        if (!value) return true;
        return data.label.indexOf(value) !== -1;
      },
      /** 查询区域信息列表 */
      getList() {
        listArea(this.queryParams).then(response => {
          this.data = response.data;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          AreaId: null,
          AreaName: null,
          AreaDesc: null,
          ParentId: null,
          OrderBy: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.AreaId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /**
       *
       * @param {节点数据} data
       * @param {点击对应的节点} node
       * @param {节点组件本身} obj
       */
      nodeClick(data,node,obj){
        this.$emit('getNode', data,node,obj);
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加区域信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const AreaId = row.AreaId || this.ids
        getArea(AreaId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改区域信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.AreaId != null) {
              updateArea(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addArea(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const AreaIds = row.AreaId || this.ids;
        this.$modal.confirm('是否确认删除区域信息编号为"' + AreaIds + '"的数据项？').then(function() {
          return delArea(AreaIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/area/export', {
          ...this.queryParams
        }, `area_${new Date().getTime()}.xlsx`)
      }
    }
  };
  </script>
